package com.hzqy.service.epg;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.WChatUtils;
import com.hzqy.web.vo.AcWinningVo;

/**
 * 边锋活动服务器中转使用
 *
 */
@Service
public class IptvActivityService {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	private final static String TV_HOST="ht"+"tp:/"+"/115.233.200.154:9010";
	private final static String TV_HOST_T="ht"+"tp:/"+"/115.233.200.155:9010";
	private final static String TV_ORDER_REPORTEX = "ht"+"tp:/"+"/115.233.200.154:9010/activity/order/reportEx";//（活动）产品包订购数据上报
	private final static String TV_ACTIVITY_REPORT = "ht"+"tp:/"+"/115.233.200.154:9010/activity/action/report";//（活动）上报签到、报名、投票行为
	private final static String TV_ADDRESS_ADD = "ht"+"tp:/"+"/115.233.200.154:9010/activity/address/add";//收货地址新增
	private final static String TV_ADDRESS_LIST = "ht"+"tp:/"+"/115.233.200.154:9010/activity/address/list";//用户收货地址列表查询
	private final static String TV_ADDRESS_edit = "ht"+"tp:/"+"/115.233.200.154:9010/activity/address/edit";//用户收货地址修改
	private final static String TV_PIOINTS_SELECTE = "ht"+"tp:/"+"/115.233.200.154:9010/activity/points/getUserPoints";//用户积分查询
	private final static String TV_PIOINTS_CONSUME = "ht"+"tp:/"+"/115.233.200.154:9010/activity/points/consume";//用户积分消费
	private final static String TV_PIOINTS_PRODUCE = "ht"+"tp:/"+"/115.233.200.154:9010/activity/points/produce";//用户积分产生
	private final static String TV_PIOINTS_ORDER = "ht"+"tp:/"+"/115.233.200.154:9010/activity/order/pointsOrder";//积分抽奖产品包订购
	private final static String TV_ORDER_REPORT = "ht"+"tp:/"+"/115.233.200.154:9010/activity/order/report";//（活动）订购积分提醒
	private final static String TV_GOODSAWARD_ADDAWARD = "ht"+"tp:/"+"/115.233.200.154:9010/activity/goodsAward/addAward";//（活动）同步中奖奖品
	private final static String TV_SYNC_AWARDRECORD = "/activity/syncAwardRecord";//新用户中奖数据同步接口

	/**
	 * 3.14上报签到、报名、投票行为
	 * @param secretKey 接口调用密钥
	 * @param iptvAccount 机顶盒账号
	 * @param activityCode 活动编码
	 * @param extraCode 附加编码
	 * @return
	 */
	public String actionReport(String secretKey,String iptvAccount,String activityCode,String extraCode)throws IOException{
		//请求上报
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("secretKey", secretKey);
		paramMap.put("iptvAccount", iptvAccount);
		paramMap.put("activityCode", activityCode);
		paramMap.put("extraCode", extraCode);
		return postCommon(paramMap,TV_ACTIVITY_REPORT,"post");
	}

	/**
	 * 3.11（活动）产品包订购数据上报
	 * @param secretKey  接口调用密钥
	 * @param iptvAccount 机顶盒账号
	 * @param productCode 订购产品编码
	 * @param startTime 订购开始时间   格式：yyyyMMddHHmmss
	 * @param endTime 订购到期时间   格式：yyyyMMddHHmmss
	 * @return
	 * @throws IOException
	 */
	public String orderReportEx(String secretKey,String iptvAccount,String productCode,String startTime,String endTime)throws IOException{
		//请求上报
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("secretKey", secretKey);
		paramMap.put("iptvAccount", iptvAccount);
		paramMap.put("productCode", productCode);
		paramMap.put("startTime", startTime);
		paramMap.put("endTime", endTime);
		return postCommon(paramMap,TV_ORDER_REPORTEX,"post");
	}
	/**
	 * 3.8（活动）收货地址新增
	 * @param secretKey
	 * @param iptvAccount
	 * @param address  地址
	 * @param receiver 收货人
	 * @param telephone 联系电话
	 * @param defaultFlag 是否默认地址，0：不是默认   1：默认
	 * @return
	 * @throws IOException
	 */
	public String activityAddressAdd(String secretKey,String iptvAccount,String receiver,String telephone,String address,String defaultFlag)throws IOException{
		//请求上报
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("secretKey", secretKey);
		paramMap.put("iptvAccount", iptvAccount);
		paramMap.put("receiver", receiver);
		paramMap.put("telephone", telephone);
		paramMap.put("address", address);
		paramMap.put("defaultFlag", defaultFlag);
		return postCommon(paramMap,TV_ADDRESS_ADD,"post");
	}

	/**
	 * 3.7（活动）收货地址查询
	 * @param secretKey
	 * @param iptvAccount
	 * @return
	 * @throws IOException
	 */
	public String activityAddressList(String secretKey,String iptvAccount)throws IOException{
		//请求上报
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("secretKey", secretKey);
		paramMap.put("iptvAccount", iptvAccount);
		return postCommon(paramMap,TV_ADDRESS_LIST,"post");
	}

	/**
	 * （活动）3.17系统用户积分查询
	 * @param secretKey
	 * @param iptvAccount
	 * @return
	 * @throws IOException
	 */
	public String activityPointsSelect(String secretKey,String iptvAccount)throws IOException{
		//请求上报
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("secretKey", secretKey);
		paramMap.put("iptvAccount", iptvAccount);
		return postCommon(paramMap,TV_PIOINTS_SELECTE,"post");
	}

	/**
	 * （活动）3.16积分消费同步
	 * @param secretKey
	 * @param iptvAccount
	 * @param activityCode 活动编码
	 * @param extraCode 附加编码
	 * @param remark 消费备注
	 * @param points 行为积分
	 * @return
	 * @throws IOException
	 */
	public String activityPointsConsume(String secretKey,String iptvAccount,String activityCode,
										String extraCode,String remark,String points)throws IOException{
		//请求上报
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("secretKey", secretKey);
		paramMap.put("iptvAccount", iptvAccount);
		paramMap.put("activityCode", activityCode);
		paramMap.put("extraCode", extraCode);
		paramMap.put("remark", remark);
		paramMap.put("points", points);
		return postCommon(paramMap,TV_PIOINTS_CONSUME,"post");
	}

	/**
	 * 3.7（活动）积分产生同步
	 * @param secretKey
	 * @param iptvAccount
	 * @param activityCode
	 * @param extraCode
	 * @param Points
	 * @return
	 * @throws IOException
	 */
	public String activityPointsProduce(String secretKey,String iptvAccount,
										String activityCode,String extraCode,String points)throws IOException{
		//请求上报
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("secretKey", secretKey);
		paramMap.put("iptvAccount", iptvAccount);
		paramMap.put("activityCode", activityCode);
		paramMap.put("extraCode", extraCode);
		paramMap.put("points", points);
		return postCommon(paramMap,TV_PIOINTS_PRODUCE,"post");
	}
	/**
	 * 积分抽奖产品包订购
	 * @param secretKey
	 * @param iptvAccount
	 * @param productCode 订购产品编码
	 * @param type 积分订购类型0：产品包；1：专区；2：单片
	 * @param startTime 订购开始时间 yyyyMMddHHmmss
	 * @param endTime 订购到期时间 yyyyMMddHHmmss
	 * @return
	 * @throws IOException
	 */
	public String activityPointsOrder(String secretKey,String iptvAccount,
									  String productCode,String type,String startTime,String endTime)throws IOException{
		//请求上报
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("secretKey", secretKey);
		paramMap.put("iptvAccount", iptvAccount);
		paramMap.put("code", productCode);
		paramMap.put("type", type);
		paramMap.put("startTime", startTime);
		paramMap.put("endTime", endTime);
		return postCommon(paramMap,TV_PIOINTS_ORDER,"post");
	}
	/**
	 * 3.13（活动）订购积分提醒
	 * @param secretKey  接口调用密钥
	 * @param iptvAccount 机顶盒账号
	 * @param productCode 订购产品编码
	 * @return
	 * @throws IOException
	 */
	public String orderReport(String secretKey,String iptvAccount,String productCode)throws IOException{
		//请求上报
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("secretKey", secretKey);
		paramMap.put("iptvAccount", iptvAccount);
		paramMap.put("productCode", productCode);
		return postCommon(paramMap,TV_ORDER_REPORT,"post");
	}
	/**
	 * 6.5（活动）同步中奖奖品
	 * @param activityCode
	 * @param iptvAccount 机顶盒账号
	 * @param goodsCode
	 * @return
	 * @throws IOException
	 */
	public String addAward(String iptvAccount,String activityCode,String goodsCode ,String secretKey)throws IOException{
		Map<String, String> paramMap = new HashMap<String, String>();
		//paramMap.put("secretKey", secretKey);
		paramMap.put("iptvAccount", iptvAccount);
		paramMap.put("activityCode", activityCode);
		paramMap.put("goodsCode", goodsCode);
		paramMap.put("secretKey", secretKey);
		return postCommon(paramMap,TV_GOODSAWARD_ADDAWARD,"post");
	}
	/**
	 * 接口调用公共类
	 * @param paramMap	携带参数
	 * @param url		请求地址
	 * @param method	请求方式  post/get
	 * @throws IOException
	 * @return String  结果需转json处理
	 */
	public static String postCommon(Map<String, String> paramMap, String url, String method) throws IOException {
		String params = "";
		for (String key : paramMap.keySet()) {
			params+="&"+ key + "=" + paramMap.get(key);
		}
		String sendResult = null;
		System.out.println("地址："+url+"?"+params);
		if(method.equals("post")){
			sendResult = WChatUtils.sendPost(url+"?"+params, "");
		}else{
			sendResult = WChatUtils.sendGet(url+"?"+params);
		}
		return sendResult;
	}
	/**
	 * 6.5（活动）同步中奖奖品
	 * @param activityCode
	 * @param iptvAccount 机顶盒账号
	 * @param goodsCode
	 * @return
	 * @throws IOException
	 */
	public String syncAwardRecord(AcWinningVo acWinningVo,String secretKey,int flag)throws IOException{
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("secretKey", secretKey);
		paramMap.put("iptvAccount", acWinningVo.getF_awl_serverid());
		paramMap.put("activityCode", acWinningVo.getF_awl_activityid());
		paramMap.put("awardCode", acWinningVo.getF_awl_prizeid());
		paramMap.put("foreignId", ""+acWinningVo.getF_awl_id());
		paramMap.put("awardName", acWinningVo.getF_awl_prizename());
		paramMap.put("customerName", acWinningVo.getF_awl_username());
		paramMap.put("customerTel", acWinningVo.getF_awl_userphone());
		paramMap.put("customerAddr", acWinningVo.getF_awl_useraddress());
		if(flag==1){
			return postCommon(paramMap,TV_HOST_T+TV_SYNC_AWARDRECORD,"post");
		}else{
			return postCommon(paramMap,TV_HOST+TV_SYNC_AWARDRECORD,"post");
		}
	}

}
